/**
 * Created by Administrator on 2017/10/6.
 */
import axios from 'axios'

console.log('ajax is loaded')
var timeout = 5000

var internalGet = function (internalUrl) {
    var start = 0
    var count = 20
    return function(reset) {
        if(reset){
            start = 0
            count = 20
        }
        return new Promise(function (resolve, rejected) {
            let url = internalUrl + "?start=" + start + "&count=" + count
            // console.log("getInTheaters: request for url " + url)
            // console.trace()
            console.log('%c internalGet: '+url, 'color:green')
            axios.get(url,{timeout})
                .then(function (response) {

                    resolve(response.data.subjects ? response.data.subjects.map(subject => {
                        return {
                            id: subject.id,
                            title: subject.title,
                            rank: subject.rating.average / 2,
                            directors: subject.directors.map(director => director.name),
                            actors: subject.casts.map(cast => cast.name),
                            poster: subject.images.medium
                        }
                    }) : [])
                    start += response.data.subjects.length
                })
                .catch(function (error) {
                    rejected(error)
                });
        })
    }
}

var getInTheaters = internalGet('/api/v2/movie/in_theaters')
var getCommingSoon = internalGet('/api/v2/movie/coming_soon')
var getMovieMsg = function (id) {
    return new Promise(function (resolve, rejected) {
        let url = '/api/v2/movie/subject/' + id + '?apikey=0b2bdeda43b5688921839c8ecb20399b&city=%E5%8C%97%E4%BA%AC&client=something&udid=dddddddddddddddddddddd'
        // console.log("getInTheaters: request for url " + url)
        // console.trace()
        console.log('%c getMovieMsg: '+url, 'color:green')
        axios.get(url,{timeout})
            .then(function (response) {

                resolve(response.data ? {
                        id: response.data.id,
                        title: response.data.title,
                        poster: response.data.images.medium,
                        rank: response.data.rating.average / 2,
                        collect_count: response.data.collect_count,
                        year: response.data.year,
                        genres:response.data.genres.join(','),
                        countries: response.data.countries.join(','),
                        pubdate: response.data.pubdates[0],
                        wish_count: response.data.wish_count,
                        reviews_count: response.data.reviews_count,
                        summary: response.data.summary,
                        staff: [
                            ...(function(directors){
                                return directors.map(function(director){
                                    return {
                                        id: director.id,
                                        name: director.name + '(导演)',
                                        photo: director.avatars.small
                                    }
                                })
                            })(response.data.directors),
                            ...(function(casts){
                                return casts.map(function(cast){
                                    return {
                                        id: cast.id,
                                        name: cast.name,
                                        photo: cast.avatars.small
                                    }
                                })
                            })(response.data.casts)
                        ],
                        smallComments: response.data.popular_comments.map(comment=>{
                            return {
                                rating: comment.rating.value,
                                created_at: comment.created_at,
                                content: comment.content,
                                author:{
                                    name: comment.author.name,
                                    photo: comment.author.avatar
                                }
                            }
                        })
                    } : null)
            })
            .catch(function (error) {
                rejected(error)
            });
    })
}
var getLongComment = function (id) {
    return new Promise(function (resolve, rejected) {
        let url = '/api/v2/movie/subject/' + id + '/reviews?apikey=0b2bdeda43b5688921839c8ecb20399b&start=0&count=20&client=something&udid=dddddddddddddddddddddd'
        // console.log("getInTheaters: request for url " + url)
        // console.trace()
        console.log('%c getLongComment: '+url, 'color:green')
        axios.get(url,{timeout})
            .then(function (response) {
                resolve({
                    title: response.data.subject.title,
                    comments: response.data.reviews.map(review => {
                                    return {
                                        title: review.title,
                                        author: {
                                            name: review.author.name,
                                            photo: review.author.avatar
                                        },
                                        created_at: review.created_at,
                                        rating: review.rating.value,
                                        summary: review.summary,
                                        approval: review.useful_count,
                                        reject: review.useless_count
                                    }
                                })}
                )
            })
            .catch(function (error) {
                rejected(error)
            })
    })
}
var getSmallComment = function (id) {
    return new Promise(function (resolve, rejected) {
        let url = '/api/v2/movie/subject/' + id + '/comments?apikey=0b2bdeda43b5688921839c8ecb20399b&count=40&client=something&udid=dddddddddddddddddddddd'
        // console.log("getInTheaters: request for url " + url)
        // console.trace()
        console.log('%c getSmallComment: '+url, 'color:green')
        axios.get(url,{timeout})
            .then(function (response) {
                resolve({
                            title: response.data.subject.title,
                            comments: response.data.comments.map(review => {
                                return {
                                    title: review.title,
                                    author: {
                                        name: review.author.name,
                                        photo: review.author.avatar
                                    },
                                    created_at: review.created_at,
                                    rating: review.rating.value,
                                    content: review.content,
                                    approval: review.useful_count
                                }
                            })
                        })
            })
            .catch(function (error) {
                rejected(error)
            })
    })
}
var getStarMsg = function (id) {
    return new Promise(function (resolve, rejected) {
        let url = '/api/v2/movie/celebrity/' + id
        // console.log("getInTheaters: request for url " + url)
        // console.trace()
        console.log('%c getStarMsg: '+url, 'color:green')
        axios.get(url,{timeout})
            .then(function (response) {
                resolve({
                    name: response.data.name,
                    name_en: response.data.name_en,
                    sex: response.data.gender,
                    photo: response.data.avatars.large,
                    works: response.data.works.map(work => {
                        return {
                            id: work.subject.id,
                            title: work.subject.title,
                            poster: work.subject.images.small,
                            roles: work.roles.join(','),
                            year: work.year,
                            rating: work.subject.rating.average / 2,
                            collect_count: work.subject.collect_count
                        }
                    })
                })
            })
            .catch(function (error) {
                rejected(error)
            })
    })
}
export {getInTheaters, getCommingSoon, getMovieMsg, getLongComment, getSmallComment, getStarMsg}